/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.FavoritesDto;
import com.scpii.universal.hibernate.dao.FavoritesManageDao;
import com.scpii.universal.service.FavoritesManageService;

/**
 * genericWeb
 * 
 * @author weny 2012-7-8下午09:43:00
 */
@Service
public class FavoritesManageServiceImpl extends
		BaseServiceImpl<FavoritesDto, Integer> implements
		FavoritesManageService {
	@Resource
	private FavoritesManageDao favoritesDao;

	@Resource
	public void setBaseDao(FavoritesManageDao favoritesDao) {
		super.setBaseDao(favoritesDao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.FavoritesService#getFavoritesRecode(java.
	 * util.Map)
	 */
	@Override
	public Integer getFavoritesRecode(FavoritesDto condition) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.FavoritesDto(t1.intFavoriteId )");
		hql.append(" from FavoritesTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");
		hql.append(" and t1.intContentId=t3.intContentId");
		hql.append(" and t1.intType=t4.intTypeId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intAppId=:intAppId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		// 查询条件
		if (condition != null) {
			if (condition.getVarContentName() != null
					&& !"".equals(condition.getVarContentName())) {
				hql.append(" and t3.varContentName like:varContentName");
				argsMap.put("varContentName",
						"%" + condition.getVarContentName() + "%");
			}
			if (condition.getIntType() != null) {
				hql.append(" and t1.intType =:intType");
				argsMap.put("intType", condition.getIntType());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		List<FavoritesDto> list = favoritesDao.hqlGetList(hql.toString(),
				argsMap);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.FavoritesService#getFavoritesListPage(java
	 * .util.Map, int, int, int)
	 */
	@Override
	public List<FavoritesDto> getFavoritesListPage(FavoritesDto condition,
			int pageNo, int pageStart, int pageRecordNo) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.FavoritesDto(t1.intFavoriteId,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");
		hql.append(" t1.datFavoriteDatetime,");
		hql.append(" t1.intType,");
		hql.append(" t1.intDeleteSign,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t4.varTypeName,  ");
		hql.append(" t2.varUserLoginName ) ");
		hql.append(" from FavoritesTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");
		hql.append(" and t1.intContentId=t3.intContentId");
		hql.append(" and t1.intType=t4.intTypeId");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intAppId=:intAppId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		// 查询条件
		if (condition != null) {
			if (condition.getVarContentName() != null
					&& !"".equals(condition.getVarContentName())) {
				hql.append(" and t3.varContentName like:varContentName");
				argsMap.put("varContentName",
						"%" + condition.getVarContentName() + "%");
			}
			if (condition.getIntType() != null) {
				hql.append(" and t1.intType =:intType");
				argsMap.put("intType", condition.getIntType());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		return favoritesDao.findByHql(hql.toString(), pageNo, pageRecordNo,
				argsMap);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.scpii.universal.service.FavoritesService#getFavoritesInfo(java.lang
	 * .Integer)
	 */
	@Override
	public FavoritesDto getFavoritesInfo(Integer intFavoriteId) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.FavoritesDto(t1.intFavoriteId,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");
		hql.append(" t1.datFavoriteDatetime,");
		hql.append(" t1.intType,");
		hql.append(" t1.intDeleteSign,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t4.varTypeName,  ");
		hql.append(" t2.varUserName ) ");
		hql.append(" from FavoritesTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");
		hql.append(" and t1.intContentId=t3.intContentId");
		hql.append(" and t1.intType=t4.intTypeId");
		hql.append(" and t1.intDeleteSign=0 ");
		hql.append(" and t1.intFavoriteId=:intFavoriteId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intFavoriteId", intFavoriteId);
		List<FavoritesDto> list = favoritesDao.hqlGetList(hql.toString(),
				argsMap);
		if (list != null) {
			return list.get(0);
		} else {
			return null;
		}
	}
}
